﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HCY.CTP.DBShangZheng;

namespace HCY.CTP.DBShangZheng
{
    public class MyDBHelper
    {
        public static List<DBTableMarketData> GetIFByData(DBTableMarketDataZi zi, out string errMsg)
        {
            DateTime dt = zi.日期.Date;
            errMsg = string.Empty;

            using (DB_WPContext db = new DB_WPContext(IFServerDLL.Properties.Settings.Default.DBConn))
            {

                var list = db.DBTableMarketData.Where(p => p.HdrID == zi.RecID).OrderBy(p => p.服务器时间).ToList();
                if (list == null)
                {
                    errMsg = "DBTableMarketData not exist.";
                    return null;
                }
                if (list.Count < 15000)
                {
                    errMsg = string.Format("DBTableMarketData count {0} less than 15000.", list.Count);
                    return null;
                }
                return list;
            }
        }
        public static DBTableMarketDataZi GetMarketZiByRecID(int recid)
        {
            using (DB_WPContext db = new DB_WPContext(IFServerDLL.Properties.Settings.Default.DBConn))
            {
                return db.DBTableMarketDataZi.Where(p => p.RecID == recid).FirstOrDefault();
            }

        }
        public static List<MyDBDataIF> GetALLDemoData()
        {
            using (DB_WPContext db = new DB_WPContext(IFServerDLL.Properties.Settings.Default.DBConn))
            {

                var sp = from p in db.DBTableMarketData
                         join q in db.DBTableMarketDataZi on p.HdrID equals q.RecID
                         select new { p.HdrID, q.合约, q.日期, q.RecID };
                var t = from p in sp
                        group p by p.HdrID into g
                        select new MyDBDataIF
                        {
                            MyIFDataCount = g.Count(),
                            Date = g.First().日期,
                            HdrID = g.Key,
                            Instrument = g.First().合约,
                            RecID = g.First().RecID,
                            MarketDataCount = db.DBTableShangZheng.Where(m => m.时间.Date.CompareTo(g.First().日期.Date) == 0).Count(),
                            MyRedGreenCount = db.DBTableDzhRedGreenbar.Where(r => r.时间.Date.CompareTo(g.First().日期.Date) == 0).Count()
                        };
                return t.Where(p=>p.MarketDataCount>100&&p.MyIFDataCount>20000&&p.MyRedGreenCount>100).ToList();
            }
        }
        public class MyDBDataIF
        {
            public int HdrID { get; set; }
            public string Instrument { get; set; }
            public DateTime Date { get; set; }
            public int RecID { get; set; }
            public int MarketDataCount { get; set; }
            public int MyIFDataCount { get; set; }
            public int MyRedGreenCount { get; set; }
        }
        public static List<DBTableMarketDataZi> GetListZi()
        {
            using (DB_WPContext db = new DB_WPContext(IFServerDLL.Properties.Settings.Default.DBConn))
            {
                return db.DBTableMarketDataZi.ToList();
            }
        }
        public static List<DBTableDzhRedGreenbar> GetRedGreenBar(DateTime dt)
        {
            using (DB_WPContext db = new DB_WPContext(IFServerDLL.Properties.Settings.Default.DBConn))
            {
                return db.DBTableDzhRedGreenbar.Where(p => p.时间.Date.CompareTo(dt.Date) == 0).ToList();
            }
        }
        public static List<DBTableShangZheng> GetMarket(DateTime dt)
        {
            using (DB_WPContext db = new DB_WPContext(IFServerDLL.Properties.Settings.Default.DBConn))
            {
                return db.DBTableShangZheng.Where(p => p.时间.Date.CompareTo(dt.Date) == 0).ToList();
            }
        }
    }
}
